feat(groupby): fast path for multi-key groupby([names]), flat + non-breaking#755
Merged
Merged
Conversation
…reaking `groupby(["a","b"]).sum()` previously dropped to the slow xarray fallback. Resolve a list of coordinate names (1-D, same dim) to a value frame so it rides the existing reindex fast path, then unstack the stacked result back into flat per-name dims -- byte-identical to the fallback, sparse fill cells included. The DataFrame grouper is untouched and stays compact (stacked MultiIndex over observed combinations only), so this is non-breaking. Flat dims are a dense cartesian grid, so a sparse key crossing materialises mostly-fill cells. Warn (pointing at the DataFrame grouper) when the grid is much larger than the observed combinations; the check reads the collapsed MultiIndex levels, so it is O(observed) and fires before unstack allocates. See #753; sparse-representation follow-ups tracked against #740. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Makes multi-key grouping by coordinate names —
expr.groupby(["a", "b"]).sum()— take the fast reindex path instead of the slow xarray fallback, while keeping its flat output and changing nothing about the existingDataFramegrouper. Implements the validated, non-breaking direction from #753.Stacked on top of #751 (
fix/groupby-coord-name) — it builds on the_resolve_grouphelper introduced there. Merge #751 first.How
unstacked back into one dim per name. The output is byte-identical to the old fallback, sparse fill cells included (verified withassert_linequal).groupby(df)(user-suppliedDataFrame) is untouched — it keeps its compact stacked-MultiIndexoutput over observed combinations only. So this is non-breaking; the tested DataFrame contract still holds.Memory note
Flat dims are a dense cartesian grid, so a sparse key crossing materialises mostly-fill cells (measured ~100× vs the DataFrame grouper for a diagonal crossing — see #740). A
UserWarningfires when the grid is much larger than the observed combinations, pointing users at the compactDataFramegrouper. The check reads the collapsedMultiIndexlevels, so it is O(observed), not O(N), and fires beforeunstackallocates the grid.This is fundamental to dense xarray (separate dims = dense grid); truly flat-and-compact needs a sparse/long-format kernel — tracked as "Future directions" on #753 and as evidence on #740.
Tests
Six new cases in
TestGroupbyByAttachedCoordinate: fast-path == fallback (list/tuple, sparse), flat-not-stacked, sparse-combination-filled, DataFrame-grouper-stays-compact, warns-when-sparse, silent-when-dense. Full groupby suite (58) + ruff + mypy green.Closes #753.
🤖 Generated with Claude Code